package com.filyar.htyd.component.webhook;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.filyar.htyd.exception.ExceptionMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class FeishuWebhook {

    @Value("${exception-robot.project-name}")
    private String PROJECT_NAME;

    @Async("async-executor")
    public void sendSystemException(ExceptionMessage  exceptionMessage){
        JSONObject contentObj = new JSONObject();
        String text = "告警时间："+ DateUtil.now()
                + "\n告警来源：" + PROJECT_NAME
                + "\n请求IP："+ exceptionMessage.getIp()
                + "\n请求城市："+ exceptionMessage.getCityInfo()
                + "\n请求地址："+ exceptionMessage.getRequestURI()
                + "\n请求方法："+ exceptionMessage.getMethod()
                + "\n请求参数："+ exceptionMessage.getQueryString()
                + "\n异常类型："+ exceptionMessage.getType()
                + "\n告警码："+ exceptionMessage.getCode()
                + "\n异常内容："+ exceptionMessage.getMessage();
        contentObj.put("text",text);
        JSONObject all = new JSONObject();
        all.put("msg_type", "text");
        all.put("content", contentObj);
        HttpRequest.post(exceptionMessage.getUrl())
                .header("Content-Type", ": application/json")
                .body(JSONObject.toJSONString(all))
                .execute();
    }

}
